package com.dsg.service.apply.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsg.common.util.UUIDGenerator;
import com.dsg.common.util.toolbox.CollectionUtil;
import com.dsg.dao.apply.DsgApplyPayMapper;
import com.dsg.entity.apply.DsgApplyBill;
import com.dsg.entity.apply.DsgApplyPay;
import com.dsg.entity.apply.DsgApplyResult;
import com.dsg.entity.apply.DsgApplyStatus;
import com.dsg.entity.dto.CusPayDto;
import com.dsg.entity.dto.CusPaymentDto;
import com.dsg.entity.set.DsgSetPayBacklist;
import com.dsg.service.DsgServiceImpl;
import com.dsg.service.apply.IDsgApplyDdaService;
import com.dsg.service.apply.IDsgApplyPayService;
import com.dsg.service.apply.IDsgApplyResultService;
import com.dsg.service.apply.IDsgApplyStatusService;
import com.dsg.service.set.IDsgSetPayBacklistService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 繳款資料 服务实现类
 * </p>
 *
 * @author system
 * @since 2020-07-04
 */
@Slf4j
@Service
public class DsgApplyPayServiceImpl extends DsgServiceImpl<DsgApplyPayMapper, DsgApplyPay> implements IDsgApplyPayService {
    @Autowired
    IDsgSetPayBacklistService payBacklistService;
    @Autowired
    IDsgApplyResultService resultService;
    @Autowired
    IDsgApplyPayService payService;
    @Autowired
    IDsgApplyDdaService ddaService;
    @Autowired
    IDsgApplyStatusService statusService;

    public Integer successInFourMonth(DsgApplyBill bill) {
        return baseMapper.successInFourMonth(bill);
    }

    public void setIsTransfer(String oid, int isTransfer) throws Exception {
        DsgApplyPay pay = getById(oid);
        if (pay.getPayMode() != DsgApplyPay.PAY_MODE_CDC) {
            throw new Exception("CDC才能設置接受轉移付款");
        }
        pay.setIsTransfer(isTransfer);
        updateById(pay);
    }

    public List<CusPaymentDto> selectCusPay(String ownsId, Boolean onProcess, String carNo, String payAccountNo) {
        return baseMapper.selectCusPay(ownsId, onProcess, carNo, payAccountNo);
    }

    public List<CusPayDto> selectPay(String ownsId) {
        return baseMapper.selectPay(ownsId);
    }

    @Override
    public boolean payAuditAll(DsgApplyBill bill) {
        ddaService.genDda(bill);

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("APP_NO", bill.getAppNo());
        List<DsgApplyPay> payList = payService.list(wrapper);
        boolean ok = true;
        log.info("审核的付款资料数：" + payList.size());
        for (DsgApplyPay pay : payList) {
            ok = audit(pay, bill.getAppNo()) && ok;
        }

        LambdaUpdateWrapper<DsgApplyStatus> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(DsgApplyStatus::getAppNo, bill.getAppNo());
        updateWrapper.set(DsgApplyStatus::getIsAuditPay, 1);
        statusService.update(new DsgApplyStatus(), updateWrapper);

        return ok;
    }

    @Override
    public boolean audit(DsgApplyPay pay, String appNo) {
        String reason = null;
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("PAY_ACCOUNT_NAME", pay.getPayAccountName());
        wrapper.eq("CUS_NO", pay.getCusNo());
        wrapper.ne("OID", pay.getOid());
        List<DsgApplyPay> someBankPays = list(wrapper);
        if (CollectionUtil.isEmpty(someBankPays)) {//该客不存在同一银行其它付款资料
            wrapper = new QueryWrapper();
            wrapper.ne("PAY_ACCOUNT_NAME", pay.getPayAccountName());
            wrapper.eq("CUS_NO", pay.getCusNo());
            DsgApplyPay otherBankPay = getOne(wrapper);
            if (otherBankPay != null) {
                int newPayMode = pay.getPayMode();
                int oldPayMode = otherBankPay.getPayMode();
                if (isDemote(newPayMode, oldPayMode)) {
                    reason = "付款方式是否降级";
                }
            }
        } else {
            for (DsgApplyPay payTemp :
                    someBankPays) {
                wrapper = new QueryWrapper();
                wrapper.eq("PAY_ACCOUNT_NO", payTemp.getPayAccountNo());
                DsgSetPayBacklist payBlacklist = payBacklistService.getOne(wrapper);
                if (payBlacklist != null) {
                    reason = "黑名单";
                }
            }
        }

        wrapper = new QueryWrapper();
        wrapper.eq("RESULT_TYPE", DsgApplyResult.RESULT_TYPE_PAY);
        wrapper.eq("APP_NO", appNo);
        wrapper.eq("RESULT_NAME", pay.getPayAccountNo());
        DsgApplyResult applyResult = resultService.getOne(wrapper);
        if (applyResult == null) {
            applyResult = new DsgApplyResult();
            applyResult.setOid(UUIDGenerator.getUUID());
            applyResult.setResultType(DsgApplyResult.RESULT_TYPE_PAY);
            applyResult.setAppNo(appNo);
            applyResult.setResultName(pay.getPayAccountNo());
        }
        applyResult.setResultReason(reason);
        applyResult.setResultStatus(reason == null ? DsgApplyResult.RESULT_STATUS_SUCCESS : DsgApplyResult.RESULT_STATUS_FAIL);
        resultService.saveOrUpdate(applyResult);

        return reason == null;
    }

    /**
     * 付款方式是否降级
     *
     * @param newPayMode
     * @param oldPayMode
     * @return
     */
    boolean isDemote(int newPayMode, int oldPayMode) {
        if ((newPayMode == DsgApplyPay.PAY_MODE_CHECK && oldPayMode != DsgApplyPay.PAY_MODE_CHECK)
                || (oldPayMode == DsgApplyPay.PAY_MODE_CDC && newPayMode != DsgApplyPay.PAY_MODE_CDC)
        ) {
            return true;
        } else {
            return false;
        }
    }
}
